﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;


namespace WebSite.admin.company
{
    public partial class CompanyUserList : BaseAdmin
    {

        //public string CompanyId
        //{
        //    get
        //    {
        //        return Request.QueryString["id"];
        //    }
        //}
        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        void DeleletCompanyUser()
        {
            if (Request.QueryString["action"] == "del")
            {
                string sSql = "update tbAdminLogin set IsDeleted=1 where Adminid = " + Request.QueryString["uid"];
                SQLAccess.DoSql(sSql);
                ExtAspNet.Alert.Show("删除成功！");
            }
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = GetTableData();
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        // Methods
        private DataTable GetTableData()
        {

            string sql = string.Format("select A.*, case a.AdminTypeId  when 1 then '业务员' else '管理员' end as AdminType  from tbAdminLogin A join tbCompanyAdmin CA on A.AdminId = CA.AdminId join tbCompany C on CA.CompanyId = C.Id "
                                        + " and isnull(A.IsDeleted,0) = 0 and ((isnull(CA.AdminTypeId,0) = isnull(A.AdminTypeId,0) and isnull(A.AdminTypeId,0) = 0)  or (isnull(CA.AdminTypeId,0) = isnull(A.AdminTypeId,0) and isnull(A.AdminTypeId,0) = 1)) and C.Id = {0}  "
                                       , Request.QueryString["Id"]);

            DataTable dtSource = null;
            DataSet ds = SQLAccess.GreatDs(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                dtSource = ds.Tables[0];
            }
            return dtSource;

        }

        void BindGrid()
        {

            DataTable table = GetTableData();
            Grid1.DataSource = table;
            Grid1.DataBind();
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyList);
                btnAddUser.OnClientClick = Window2.GetShowReference("addCompanyUser.aspx?id=" + Request.QueryString["id"], "添加商家用户");
                this.DeleletCompanyUser();
                this.BindGrid();
            }
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGrid();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddCompanyUser.aspx?id=" + Request.QueryString["id"]);
        }

        protected void Window2_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {
            this.BindGrid();
        }


    }
}